﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using GUI_Web.UserWebService1;
using BL;

namespace GUI_Web
{
    public partial class ListPessoa : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Service1 service = new Service1();
            //DataTable table = service.getUserData();
            carregaGrid();
            
        }

        private void carregaGrid()
        {
            DataTable table = new DataTable();

            try
            {
                table = PessoaBL.Lista();

                if (table.Rows.Count > 0)
                {
                    gvPessoa.DataKeyNames = new string[] { "id" };
                    gvPessoa.DataSource = table;
                    gvPessoa.DataBind();
                }
                else
                {
                    gvPessoa.DataSource = null;
                    gvPessoa.DataBind();
                }

                gvPessoa.DataSource = table;
                gvPessoa.DataBind();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        protected void gvPessoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = gvPessoa.SelectedIndex;

            Response.Redirect("CadPessoa.aspx");
        }

        protected void gvPessoa_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            gvPessoa.PageIndex = e.NewSelectedIndex;
            carregaGrid();
        }

        protected void gvPessoa_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "alterar")
                Response.Redirect("CadPessoa.aspx?idPessoa=" + e.CommandArgument);
        }

    }
}